import axios from 'axios';
import * as escfCity from './../data/escfCity';


// const getBaseUrl = 'http://121.40.41.188:8080/car/api1'
// const postBaseUrl = 'http://121.40.41.188:8080/car/api2'
// const postBaseUrl2 = 'http://121.40.41.188:8080/car/api3'
// const getBaseUrl2 = 'http://121.40.41.188:8080/car/api4'

const getBaseUrl = 'http://www.bjdd301.cn:80/car/api1'
const postBaseUrl = 'http://www.bjdd301.cn:80/car/api2'
const postBaseUrl2 = 'http://www.bjdd301.cn:80/car/api3'
const getBaseUrl2 = 'http://www.bjdd301.cn:80/car/api4'

const baseGet = (url, callback) => {
  axios.request({
    url: url,
    method: 'get',
    timeout: 10000
  }).then(function (res) {
    callback(res)
  }).catch(function (error) {
    var result = {
      error: true
    }
    // console.log(result)
    callback(result)
  })
}

// 获取二手车之家数据
export const getEscFamily = (_self, callback) => {
  var url = getBaseUrl2 + '?url=' + _self.$const.ESCFAMILYPPURL
  baseGet(url, (res) => {
    if (!res.error) {
      analysisCCWData(_self, res, (result) => {
        callback(result)
      })
    } else {
      callback(res)
    }
  })
}
// 解析二手车之家数据
const analysisCCWData = (_self, res, callback) => {
    let data = res.data
    let ccwData = {}
    if (!data.error) {
      let ppStr = eval(data.data)
      let brandList = ppStr.split(',')
      let zimuList = []
      let list = []
      let itemList = new Array()
      let brandName = ''
      let zimu = 'A'
      zimuList.push(zimu)
      for (var i = 0; i < brandList.length; i = i + 2) {
        brandName = brandList[i] == 35 ? '阿斯顿·马丁' : brandList[i + 1].substring(2)
        let curZimu = brandList[i + 1].substring(0, 1)
        let item = {
          b_id: brandList[i],
          pp_name: brandName,
          zimu: curZimu
        }
        if (zimu == curZimu) {
          itemList.push(item)
        } else {
          var itemDetail = {
            listByZimu: itemList,
            zimu: zimu,
            id: 'brand_' + zimu
          }
          list.push(itemDetail)
          zimu = curZimu
          zimuList.push(zimu)
          itemList = new Array()
          itemList.push(item)
        }
      }
      ccwData = {
        error: false,
        name: '二手车之家',
        list: list,
        zimuList: zimuList
      }
    } else {
      ccwData = {
        error: true
      }
    }
    callback(ccwData)
}

// 获取车虫网数据
export const getCcwData = (_self, callback) => {
  var url = getBaseUrl + '?url=' + _self.$const.CCWURL
  baseGet(url, (res) => {
    console.log(res)
    if (res.error) {
      callback(res)
    } else {
      callback(res.data)
    }
  })
}
//解析车虫网数据
export const analysisCcwData = (obj, res, callback) => {
  res = res.replace(/\<script((.|\n)*?)\<\/script\>/gi, '')
  res = res.replace(/\<link((.|\n)*?)\/>/gi, '')
  res = res.replace(/\<head((.|\n)*?)\<\/head\>/gi, '')
  res = res.replace(/<\s?img[^>]*>/gi, '')
  obj.html(res)
  let elements = obj.find('.pingpai_list_r ul')
  let list = []
  let zimuList = []
  for (let i = 0; i < elements.length; i++) {
    let curUl = elements.eq(i)
    let curId = curUl.attr("id")
    let zimu = curId.substring(curId.length - 1)
    if (curId !== 'brand_') {
      zimuList.push(zimu)
      let allLi = curUl.find('li')
      let itemList = new Array()
      for (var k = 0; k < allLi.length; k++) {
        let curLi = allLi.eq(k)
        let b_id = curLi.attr('data')
        let pp_name = curLi.attr('data-name')
        let item = {
          b_id: b_id,
          pp_name: pp_name,
          zimu: zimu
        }
        itemList.push(item)
      }
      var itemDetail = {
        listByZimu: itemList,
        zimu: zimu,
        id: 'brand_' + zimu
      }
      list.push(itemDetail)
    } else {
      continue
    }
  }
  obj.html('')
  var ccwData = {
    error: false,
    name: '车虫网',
    list: list,
    zimuList: zimuList
  }
  callback(ccwData)
}


// 获取车300数据
export const getCThreeHundred = (_self, callback) => {
  var url = getBaseUrl + '?url=' + _self.$const.CTHURL
  baseGet(url, (res) => {
    // console.log('ch300')
    // console.log(res)
    if (res.error) {
      callback(res)
      return
    } 
    callback(res.data)
  })
}

export const analysisCTHData = (obj, res, callback) => {
  // console.log(res)
  res = res.replace(/\<script((.|\n)*?)\<\/script\>/gi, '')
  res = res.replace(/<script[\s\S]+?<\/script>/gi, '')
  res = res.replace(/\<link((.|\n)*?)\/>/gi, '')
  res = res.replace(/\<head((.|\n)*?)\<\/head\>/gi, '')
  res = res.replace(/<\s?img[^>]*>/gi, '')
  obj.html(res)

  var elements = obj.find('.brandgun').eq(0).find('p')

  let list = new Array()
  let zimuList = new Array()
  let itemList = new Array()
  let zimu = 'A'
  zimuList.push(zimu)
  for (var i = 0; i < elements.length; i++) {
    let curEl = elements.eq(i)
    let className = curEl.attr('class')
    if (className.indexOf('list_1') !== -1) {
      let item = {
        b_id: curEl.attr('id'),
        pp_name: curEl.html(),
        zimu: zimu
      }
      itemList.push(item)
    } else {
      var curZimu = curEl.attr('id')
      if (curZimu !== zimu) {
        var itemDetail = {
          listByZimu: itemList,
          zimu: zimu,
          id: 'brand_' + zimu
        }
        list.push(itemDetail)
        zimu = curZimu
        zimuList.push(zimu)
        itemList = new Array()
      }
    }
  }
  list.push(itemList)
  let ccwData = {
    name: '车300',
    list: list,
    zimuList: zimuList
  }
  obj.html('')
  callback(ccwData)
}

// 获取子品牌
export const getCcwChildPP = (data, _self, callback) => {
  var url = postBaseUrl + '?url=' + _self.$const.CCWCHILDPPURL + '&data=' + data
  baseGet(url, (res) => {
    // console.log(res.data)
    if (!res.data.error) {
      let result = eval("("+res.data.data+")")
      // let result = JSON.parse(res.data.data)
      result = result.series
      let list = new Array()
      let itemList = new Array()
      let header = {}
      let first = true
      for (var i = 0; i < result.length; i++) {
        let item = result[i]
        if (item.isshow == 0) {
          if (first) {
            first = false
            header = item
            continue
          }
          let itemObject = {
            header: header,
            itemList: itemList
          }
          list.push(itemObject)
          itemList = new Array()
        } else {
          itemList.push(item)
        }
      }
      let itemObject = {
        header: header,
        itemList: itemList
      }
      list.push(itemObject)
      var resultObj = {
        error: false,
        list: list
      }
      callback(resultObj)
    }
  })
}

export const getCthChildPP = (data, _self, callback) => {
  var url = getBaseUrl + '?url=' + _self.$const.CTHCHILDPPURLL + data + '.json'
  baseGet(url, (res) => {
    let curData = eval("("+res.data.data+")") 
    let itemList = new Array()
    let list = new Array()
    let header = new Object()
    for (var i = 0; i < curData.length; i++) {
      let item = curData[i]
      if (i == 0) {
        header.name = item.series_group_name
        item.id = item.series_id
        item.name = item.series_name
        itemList.push(item)
        continue
      } else {
        if (header.name === item.series_group_name) {
          item.id = item.series_id
          item.name = item.series_name
          itemList.push(item)
        } else {
          let itemObj = {
            header: header,
            itemList: itemList
          }
          list.push(itemObj)
          itemList = new Array()
          header = new Object()
          header.name = item.series_group_name
          item.id = item.series_id
          item.name = item.series_name
          itemList.push(item)
        }
      }
    }
    let itemObj = {
      error: false,
      header: header,
      itemList: itemList
    }
    list.push(itemObj)
    callback(list)
  })
}

export const getCFChildPP = (data, _self, callback) => {
  var url = getBaseUrl2 + "?url=" + _self.$const.ESCFAMILYCHILDPPURL + data
  // console.log(url)
  baseGet(url, (res) => {
    let ppStr = eval(res.data.data).split(',')
    let list = new Array()
    let itemList = new Array()
    let header = {}
    for (var i = 0 ; i < ppStr.length; i += 2) {
      let cur = ppStr[i + 1].split(' ')
      let item = {
        id: ppStr[i],
        series_group_name: cur[0]
      }
      let cur_name = ''
      for (var k = 1; k < cur.length; k ++) {
        if (k !== cur.length - 1) {
          cur_name += cur[k] + ' '
        } else {
          cur_name += cur[k]
        }
      }
      item.name = cur_name
      if (i === 0) {
        header.name = cur[0]
        itemList.push(item)
        continue
      } else {
        if (header.name === cur[0]) {
          itemList.push(item)
        } else {
          let itemObject = {
            header: header,
            itemList: itemList
          }
          list.push(itemObject)
          header = new Object()
          header.name = cur[0]
          itemList = new Array()
          itemList.push(item)
        }
      }
    }
    let itemObject = {
      error: false,
      header: header,
      itemList: itemList
    }
    list.push(itemObject)
    callback(list)
  })
}


// 获取子品牌Simple
export const getCcwChildPPSimple = (data, _self, callback) => {
  var url = postBaseUrl2 + '?url=' + _self.$const.CCWSIMPLEURL + '&' + data
  baseGet(url, (res) => {
    callback(eval("("+res.data.data+")") )
  })
}

export const getCthChildPPSimple = (data, _self, callback) => {
  var url = getBaseUrl + '?url=' + _self.$const.CTHSIMPLEURL + data + '.json'
  baseGet(url, (res) => {
    var curData = eval("("+res.data.data+")") 
    let itemList = new Array()
    let list = new Array()
    let timeLsit = new Array()
    let model_time = ''
    for (var i = 0; i < curData.length; i++) {
      let item = curData[i]
      item.id = item.model_id
      item.name = item.model_name
      item.new_price = item.model_price
      if (i == 0) {
        model_time = item.model_year
        itemList.push(item)
        continue
      } else {
        if (model_time === item.model_year) {
          itemList.push(item)
        } else {
          let itemObj = {
            model_time: model_time,
            itemList: itemList
          }
          timeLsit.push(model_time)
          list.push(itemObj)
          itemList = new Array()
          model_time = item.model_year
          itemList.push(item)
        }
      }
    }
    let itemObj = {
      model_time: model_time,
      itemList: itemList
    }
    list.push(itemObj)
    timeLsit.push(model_time)
    var resData = {
      error: false,
      list: list,
      timeLsit: timeLsit
    }
    callback(resData)
  })
}

// 获取二手车之家
export const getCFChildPPSimple = (data, _self, callback) => {
  var url = getBaseUrl2 + '?url=' + _self.$const.ESCFAMILYSIMPLEURL + data
  baseGet(url, (res) => {
    let ppStr = eval(res.data.data)
    let itemList = new Array()
    let list = new Array()
    let timeLsit = new Array()
    let model_time = ''
    
    for (var i = 0; i < ppStr.length; i ++) {
      var cur = ppStr[i]
      if (cur.year) {
        model_time = parseInt(cur.year)
        timeLsit.push(model_time)
        var itemObject = {
          model_time: model_time,
          itemList: cur.spec
        }
      } else {
        model_time = '未知'
        timeLsit.push(model_time)
        var itemObject = {
          model_time: '未知',
          itemList: cur.spec
        }
      }
      list.push(itemObject)
    }
    var resData = {
      error: false,
      list: list,
      timeLsit: timeLsit
    }
    callback(resData)
  })
}


// 获取车300的城市数据
export const getCthCityData = (obj, _self, callback) => {
  var url = getBaseUrl + '?url=' + _self.$const.CTHCITYURL
  baseGet(url, (res1) => {
    // console.log(res1)
    var url2 = getBaseUrl + '?url=' + _self.$const.CTHURL
    baseGet(url2, (res2) => {
      // console.log(res2)
      var list = analysisCthCityData(obj, eval("("+res1.data.data+")") , res2.data.data)
      callback(list)
    })
  })
}

const analysisCthCityData = (obj, res1, res2) => {
  var list = []
  res2 = res2.replace(/\<script((.|\n)*?)\<\/script\>/gi, '')
  res2 = res2.replace(/<script[\s\S]+?<\/script>/gi, '')
  res2 = res2.replace(/\<link((.|\n)*?)\/>/gi, '')
  res2 = res2.replace(/\<head((.|\n)*?)\<\/head\>/gi, '')
  res2 = res2.replace(/<\s?img[^>]*>/gi, '')
  obj.html(res2)

  var elements = obj.find('.select_province').eq(0).find('p')

  for (var i = 0; i < elements.length; i++) {
    var curEle = elements.eq(i)
    var item = {
      id: curEle.attr('id'),
      name: curEle.html(),
      parentId: 0,
      i: '',
      level: 1
    }
    list.push(item)
  }
  var curData = res1
  for (var i = 0; i < curData.length; i++) {
    var cur = curData[i]
    var item = {
      id: cur.city_id,
      name: cur.city_name,
      parentId: cur.prov_id,
      i: cur.initial,
      level: 2
    }
    list.push(item)
  }
  // console.log(JSON.stringify(list))
  obj.html('')
  return list
}


// 获取二手车之家的城市数据
export const getEscfCityData = (_self, callback) => {
  let cityJson = escfCity.cityJson
  let Province_2sc = escfCity.Province_2sc
  let City_2sc = escfCity.City_2sc
  let list = new Array()
  for (var i = 0; i < Province_2sc.options.length; i ++) {
    if (Province_2sc.options[i].V == 820000 || Province_2sc.options[i].V == 810000 || Province_2sc.options[i].V == 710000) 
      continue
    let cur = Province_2sc.options[i]
    let item = {
      id: cur.V,
      parentId: 0,
      name: cur.T.substring(2, cur.T.length),
      level: 1
    }
    list.push(item)
  }

  for (var i = 0; i < City_2sc.options.length; i ++) {
    let cur = City_2sc.options[i]
    let item = {
      id: cur.V,
      parentId: cur.P,
      name: cur.T.substring(2, cur.T.length),
      level: 2
    }
    list.push(item)
  }
  
  callback(list)
}